/*
 * Copyright (c) 2013 哈尔滨亿时代数码科技开发有限公司（www.hrbesd.com）. All rights reserved.
 * 
 * HRBESD PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */ 
package com.esd.ps;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.esd.db.model.VtdArticle;
import com.esd.db.model.VtdComment;
import com.esd.db.service.ArticleService;

/**
 * 交流区管理
 */
@Controller
@RequestMapping("/security")
public class ArticleController {
	private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);
	
	@Autowired
	private ArticleService articleService;
	

	/**
	 * 跳转到文章发布页
	 */
	@RequestMapping(value = "/createArticle", method = RequestMethod.GET)
	public ModelAndView createArticle() {
		return new ModelAndView("article/createArticle");
	}
	
	/**
	 * 上传图片
	 * 
	 * @param noteImage
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/uploadArticleImage", method = RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> uploadArticleImagePOST(@RequestParam(value = "upfile", required = false) MultipartFile image, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		String imageName = null;
		
		if (image.isEmpty()) {
			return map;
		}
		String url = request.getSession().getServletContext().getRealPath(Constants.SLASH);
		int psIndex = url.lastIndexOf("ps");
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String dateFileDir = df.format(new Date());
 		String[] fileDirs = dateFileDir.split("-");
        url = url.substring(0, psIndex);
        url = url + "uploadArticleImages"+File.separatorChar+fileDirs[0]+File.separatorChar+fileDirs[1]+File.separatorChar+fileDirs[2];
		try {
			File f = new File(url);
			if (!f.exists()) {
				f.mkdirs();
			}
			String uuid = UUID.randomUUID().toString();
			imageName = image.getOriginalFilename();

			String str[] = imageName.split("\\.");
			imageName = uuid + Constants.POINT +str[1];
			File uploadFile = new File(url, imageName);
			InputStream is = image.getInputStream();
			FileOutputStream fos = new FileOutputStream(uploadFile);
			byte[] tmp = new byte[1024];
			int len = -1;
			while ((len = is.read(tmp)) != -1) {
				fos.write(tmp, 0, len);
			}
			is.close();
			fos.flush();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String wrongPath = "/uploadArticleImages/"+fileDirs[0]+"/"+fileDirs[1]+"/"+fileDirs[2]+"/"+ imageName;
		map.clear();
		map.put(Constants.ORIGINALNAME, imageName);
		map.put(Constants.NAME, imageName);
		map.put(Constants.URL, wrongPath);
		map.put(Constants.SIZE, image.getSize());
		map.put(Constants.TYPE, imageName.substring(imageName.indexOf(Constants.POINT) + 1, imageName.length()));
		map.put(Constants.STATE, Constants.SUCCESS);
		return map;
	}
	
	/**
	 * 创建新文章  --一般用户
	 */
	@RequestMapping(value = "/saveArticle", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveArticlePOST(String title, String content, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int operatorId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		VtdArticle article = new VtdArticle();
		article.setId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
		article.setArticleTitle(title);
		article.setArticleContent(content);
		article.setCreateId(operatorId);
		article.setCreateUsername(session.getAttribute(Constants.USER_NAME).toString());
		article.setUpdateId(operatorId);
		int num = articleService.saveArticle(article);
		if(num==1){
			map.put(Constants.REPLAY, 1);
			map.put(Constants.MESSAGE, "保存成功,待审核通过后即可在交流区中查看!");
		}else{
			map.put(Constants.REPLAY, 0);
			map.put(Constants.MESSAGE, "保存失败,请联系系统维护人员!");
		}
		return map;
	}
	
	/**
	 * 查询待审核文章数
	 */
	@RequestMapping(value = "/getWaitInspArticleNum", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getWaitInspArticleNum(HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int num = articleService.getWaitInspArticleCount();
		if(num==0){
			map.put("num", "");
		}else{
			map.put("num", num);
		}
		map.put(Constants.REPLAY, 1);
		return map;
	}
	
	/**
	 * 跳转到文章审核列表页
	 */
	@RequestMapping(value = "/articleInsp", method = RequestMethod.GET)
	public ModelAndView articleInsp() {
		return new ModelAndView("article/articleInspList");
	}
	
	/**
	 * 查询待审核文章
	 */
	@RequestMapping(value = "/waitInspArticleList", method = RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> waitInspArticleList(String title,String createUsername, int page) {
		logger.debug("title:{}", title);
		logger.debug("createUsername:{}", createUsername);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String,Object>> result = new ArrayList<>();
		List<VtdArticle> articleList = articleService.getArticleListByCondition(title, createUsername, 0, 0, 
				null, null, page, Constants.ROW);

		SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATETIME_FORMAT);		
		for (VtdArticle article : articleList) {
			Map<String,Object> temp = new HashMap<String,Object>();
			temp.put("id", article.getId());
			temp.put("articleTitle", article.getArticleTitle());
			temp.put("createTime", sdf.format(article.getCreateTime()));
			temp.put("updateTime", sdf.format(article.getUpdateTime()));
			temp.put("createUsername", article.getCreateUsername());
			result.add(temp);
		}
		int totle = articleService.getArticleListByConditionCount(title, createUsername, 0, 0, null, null);
		int waitInspArticleCount = articleService.getWaitInspArticleCount();
		if(waitInspArticleCount==0){
			map.put("waitInspArticleCount", "");
		}else{
			map.put("waitInspArticleCount", waitInspArticleCount);
		}		
		if(result.isEmpty()){
			map.put(Constants.LIST, "");
		}else{
			map.put(Constants.LIST, result);
		}
		map.put(Constants.TOTLE, totle);
		map.put(Constants.TOTLE_PAGE, Math.ceil((double) totle / (double) Constants.ROW));
		return map;
	}
	
	/**
	 * 修改文章审核状态
	 */
	@RequestMapping(value = "/changeDeleteStatus", method = RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> deleteArticle(String id, int isDelete, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int userId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		int num = articleService.changeDeleteStatus(id, isDelete, userId);
		if(num==1){
			map.put(Constants.REPLAY, 1);
			map.put(Constants.MESSAGE, "删除状态修改成功!");
		}else{
			map.put(Constants.REPLAY, 0);
			map.put(Constants.MESSAGE, "删除状态修改失败,请联系系统维护人员!");
		}
		return map;
	}
	
	/**
	 * 查看文章内容
	 */
	@RequestMapping(value = "/articleStatusEdit", method = RequestMethod.GET)
	public  ModelAndView articleStatusEdit(String id) {
		VtdArticle article = articleService.selectById(id);	
		return new ModelAndView("article/articleStatusEdit", "article", article);
	}
	
	/**
	 * 查看文章内容
	 */
	@RequestMapping(value = "/showArticleContent", method = RequestMethod.GET)
	public  ModelAndView showArticleContent(String id) {
		ModelAndView mv = new ModelAndView();
		VtdArticle article = articleService.selectById(id);	
		List<VtdComment> list = articleService.findCommentsByArticleId(id);
		List<Map<String, Object>> resutlList = new ArrayList<Map<String,Object>>();
		for (VtdComment comment : list) {
			Map<String,Object> temp = new HashMap<String,Object>();
			//查询评论的回复内容
			List<VtdComment> commentReplyList = articleService.findCommentReplysByPid(comment.getId());
			temp.put("comment", comment);
			temp.put("replyNum", commentReplyList.size());
			temp.put("replyList", commentReplyList);
			resutlList.add(temp);
		}
		//查询这篇文章的回复总数
		int totleCommentNum;
		if(article!=null){
			totleCommentNum = articleService.getTotleCommentNum(article.getId());
		}else{
			totleCommentNum = 0;
		}
		mv.setViewName("article/showArticle");
		mv.addObject("article", article);
		mv.addObject("commentList",resutlList);
		mv.addObject("totleCommentNum",totleCommentNum);
		return mv;
	}
	
	
	/**
	 * 文章审核通过
	 */
	@RequestMapping(value = "/changeInspStatus", method = RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> changeInspStatus(String id, int isGranted, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int userId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		int num = articleService.changeInspStatus(id, isGranted, userId);
		if(num==1){
			map.put(Constants.REPLAY, 1);
			map.put(Constants.MESSAGE, "审核状态已修改!");
		}else{
			map.put(Constants.REPLAY, 0);
			map.put(Constants.MESSAGE, "审核状态修改失败,请联系系统维护人员!");
		}
		return map;
	}
	
	/**
	 * 修改置顶状态
	 */
	@RequestMapping(value = "/changeTopStatus", method = RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> changeTopStatus(String id, int isTop, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int userId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		int num = articleService.changeTopStatus(id, isTop, userId);
		if(num==1){
			map.put(Constants.REPLAY, 1);
			map.put(Constants.MESSAGE, "置顶状态已修改!");
		}else{
			map.put(Constants.REPLAY, 0);
			map.put(Constants.MESSAGE, "置顶状态修改失败,请联系系统维护人员!");
		}
		return map;
	}
	
	/**
	 * 修改评论开启状态
	 */
	@RequestMapping(value = "/changeLockStatus", method = RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> changeLockStatus(String id, int isLock, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int userId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		int num = articleService.changeLockStatus(id, isLock, userId);
		if(num==1){
			map.put(Constants.REPLAY, 1);
			map.put(Constants.MESSAGE, "评论开启状态修改成功!");
		}else{
			map.put(Constants.REPLAY, 0);
			map.put(Constants.MESSAGE, "评论开启状态修改失败,请联系系统维护人员!");
		}
		return map;
	}
	
	/**
	 * 跳转到文章发布页
	 */
	@RequestMapping(value = "/managerCreateArticle", method = RequestMethod.GET)
	public ModelAndView managerCreateArticle() {
		return new ModelAndView("article/managerCreateArticle");
	}
	
	
	/**
	 * 查询文章列表
	 */
	@RequestMapping(value = "/loadArticles", method = RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> loadArticles(String title,String createUsername, int page) {
		logger.debug("title:{}", title);
		logger.debug("createUsername:{}", createUsername);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String,Object>> result = new ArrayList<>();
		List<Map<String,Object>> topResult = new ArrayList<>();
		// 普通文章列表
		List<VtdArticle> articleList = articleService.getArticleListByCondition(title, createUsername, 0, 1, 
				null, 0, page, Constants.ROW);
		// 置顶文章列表 只显示最近发布的10条信息
		List<VtdArticle> topArticleList = articleService.getArticleListByCondition("", "", 0, 1, 
				null, 1, 1, 10);
		SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATETIME_FORMAT);	
		// 组装普通列表显示数据
		for (VtdArticle article : articleList) { 
			Map<String,Object> temp = new HashMap<String,Object>();
			temp.put("id", article.getId());
			temp.put("articleTitle", article.getArticleTitle());
			temp.put("createTime", sdf.format(article.getCreateTime()));
			temp.put("updateTime", sdf.format(article.getUpdateTime()));
			temp.put("createUsername", article.getCreateUsername());
			temp.put("isLocked", article.getIsLocked());
			Map<String,Object> openLogNumAndCommentNum = articleService.queryOpenLogNumAndCommentNumByArticleId(article.getId());
			temp.putAll(openLogNumAndCommentNum);
			result.add(temp);
		}
		// 组装置顶列表显示数据
		for (VtdArticle a : topArticleList) {
			Map<String,Object> temp = new HashMap<String,Object>();
			temp.put("id", a.getId());
			temp.put("articleTitle", a.getArticleTitle());
			temp.put("createTime", sdf.format(a.getCreateTime()));
			temp.put("updateTime", sdf.format(a.getUpdateTime()));
			temp.put("createUsername", a.getCreateUsername());
			temp.put("isLocked", a.getIsLocked());
			Map<String,Object> openLogNumAndCommentNum = articleService.queryOpenLogNumAndCommentNumByArticleId(a.getId());
			temp.putAll(openLogNumAndCommentNum);
			topResult.add(temp);
		}
		
		int totle = articleService.getArticleListByConditionCount(title, createUsername, 0, 1, null, 0);	
		if(result.isEmpty()){
			map.put(Constants.LIST, "");
		}else{
			map.put(Constants.LIST, result);
		}
		if(topResult.isEmpty()){
			map.put("topArticleList", "");
		}else{
			map.put("topArticleList", topResult);
		}
		map.put(Constants.TOTLE, totle);
		map.put(Constants.TOTLE_PAGE, Math.ceil((double) totle / (double) Constants.ROW));
		return map;
	}
	
	/**
	 * 创建新文章  --管理用户
	 */
	@RequestMapping(value = "/managerSaveArticle", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> managerSaveArticle(String title, String content, int isTop, int isLocked, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int operatorId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		VtdArticle article = new VtdArticle();
		article.setId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
		article.setArticleTitle(title);
		article.setArticleContent(content);
		article.setCreateId(operatorId);
		article.setCreateUsername(session.getAttribute(Constants.USER_NAME).toString());
		article.setUpdateId(operatorId);
		article.setIsGranted(1);
		article.setIsTop(isTop);
		article.setIsLocked(isLocked);
		int num = articleService.managerSaveArticle(article);
		if(num==1){
			map.put(Constants.REPLAY, 1);
			map.put(Constants.MESSAGE, "保存成功,可在交流区中查看!");
		}else{
			map.put(Constants.REPLAY, 0);
			map.put(Constants.MESSAGE, "保存失败,请联系系统维护人员!");
		}
		return map;
	}
	
	/**
	 * 跳转到当前账号发布的文章列表页
	 */
	@RequestMapping(value = "/ownedArticleList", method = RequestMethod.GET)
	public ModelAndView ownedArticleListGet() {
		return new ModelAndView("article/ownedArticleList");
	}
	
	/**
	 * 查询当前登录账号自己的文章列表
	 */
	@RequestMapping(value = "/ownedArticleList", method = RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> ownedArticleList(String title, int page,  HttpSession session) {
		int userId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String,Object>> result = new ArrayList<>();
		List<VtdArticle> articleList = articleService.getArticleListByUserId(title, userId, page, Constants.ROW);
		SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATETIME_FORMAT);		
		for (VtdArticle article : articleList) {
			Map<String,Object> temp = new HashMap<String,Object>();
			temp.put("id", article.getId());
			temp.put("articleTitle", article.getArticleTitle());
			temp.put("createTime", sdf.format(article.getCreateTime()));
			temp.put("updateTime", sdf.format(article.getUpdateTime()));
			temp.put("createUsername", article.getCreateUsername());
			result.add(temp);
		}
		int totle = articleService.getArticleListByUserIdCount(userId,title);
		if(result.isEmpty()){
			map.put(Constants.LIST, "");
		}else{
			map.put(Constants.LIST, result);
		}
		map.put(Constants.TOTLE, totle);
		map.put(Constants.TOTLE_PAGE, Math.ceil((double) totle / (double) Constants.ROW));
		return map;
	}
	
	/**
	 * 跳转到编辑文章页面
	 */
	@RequestMapping(value = "/editArticle", method = RequestMethod.GET)
	public  ModelAndView editArticleGet(String id) {
		VtdArticle article = articleService.selectById(id);	
		return new ModelAndView("article/editArticle", "article", article);
	}
	
	/**
	 * 编辑文章
	 */
	@RequestMapping(value = "/editArticle", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> editArticlePost(String title, String content, String id , HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int operatorId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		int num = articleService.updateTitleAndContentByArticleId(title,content,id,operatorId);
		if(num==1){
			map.put(Constants.REPLAY, 1);
			map.put(Constants.MESSAGE, "编辑成功,可在交流区中查看!");
		}else{
			map.put(Constants.REPLAY, 0);
			map.put(Constants.MESSAGE, "编辑失败,请联系系统维护人员!");
		}
		return map;
	}
	
	/**
	 * 保存评论
	 */
	@RequestMapping(value = "/saveComment", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveComment(String articleId, int targetUserId, String targetUsername, String content, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int operatorId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		VtdComment comment = new VtdComment();
		comment.setId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
		comment.setArticleId(articleId);
		comment.setTargetUserId(targetUserId);
		comment.setTargetUsername(targetUsername);
		comment.setContent(content);
		comment.setIsCommentReply(0);
		comment.setPid("-");
		comment.setCreateUserId(operatorId);
		comment.setCreateUsername(session.getAttribute(Constants.USER_NAME).toString());
		int num = articleService.saveComment(comment);
		if(num==1){
			map.put(Constants.REPLAY, 1);
			map.put(Constants.MESSAGE, "评论成功,待审核通过后即可在文章中查看!"); 
		}else{
			map.put(Constants.REPLAY, 0);
			map.put(Constants.MESSAGE, "评论失败,请联系系统维护人员!");
		}
		return map;
	}
	
	/**
	 * 保存评论的回复
	 */
	@RequestMapping(value = "/saveCommentReply", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveCommentReply(String articleId, String targetId, String content, int isCommentReply, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int operatorId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		VtdComment target = articleService.findCommentById(targetId); //查询被回复的评论
		//保存新的
		if(target!=null){
			VtdComment comment = new VtdComment();
			comment.setId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
			comment.setArticleId(articleId);
			comment.setTargetUserId(target.getCreateUserId());
			comment.setTargetUsername(target.getCreateUsername());
			comment.setContent(content);
			comment.setIsCommentReply(1);
			if(isCommentReply == 1){  //如果回复的是评论的回复的回复  和被回复的限制在同一个pid下
				comment.setPid(target.getPid());
			}else{
				comment.setPid(targetId);
			}
			comment.setCreateUserId(operatorId);
			comment.setCreateUsername(session.getAttribute(Constants.USER_NAME).toString());
			int num = articleService.saveComment(comment);
			if(num==1){
				map.put(Constants.REPLAY, 1);
				map.put(Constants.MESSAGE, "回复成功,待审核通过后即可在文章中查看!!");
			}else{
				map.put(Constants.REPLAY, 0);
				map.put(Constants.MESSAGE, "回复失败,请联系系统维护人员!");
			}
		}else{
			map.put(Constants.REPLAY, 0); //文章都没了 回复成功也看不到的
			map.put(Constants.MESSAGE, "回复出错,请联系系统维护人员!!");
		}
		return map;
	}
	
	/**
	 * 跳转到评论审核列表页
	 */
	@RequestMapping(value = "/commentInsp", method = RequestMethod.GET)
	public ModelAndView commentInsp() {
		return new ModelAndView("article/commentInspList");
	}
	
	/**
	 * 查询待审核的评论
	 */
	@RequestMapping(value = "/waitInspCommentList", method = RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> waitInspCommentList(int page) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String,Object>> result = new ArrayList<>();
		List<VtdComment> CommentList = articleService.getCommentListForInspection(page, Constants.ROW);
		SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATETIME_FORMAT);		
		for (VtdComment comment : CommentList) {
			Map<String,Object> temp = new HashMap<String,Object>();
			temp.put("id", comment.getId());
			temp.put("commentContent", comment.getContent());
			temp.put("createTime", sdf.format(comment.getCreateTime()));
			temp.put("createUsername", comment.getCreateUsername());
			result.add(temp);
		}
		//查询待审核总数
		int totle = articleService.getCommentListForInspectionCount();
		if(totle==0){
			map.put("waitInspCommentCount", "");
		}else{
			map.put("waitInspCommentCount", totle);
		}		
		if(result.isEmpty()){
			map.put(Constants.LIST, "");
		}else{
			map.put(Constants.LIST, result);
		}
		map.put(Constants.TOTLE, totle);
		map.put(Constants.TOTLE_PAGE, Math.ceil((double) totle / (double) Constants.ROW));
		return map;
	}
	
	/**
	 * 删除评论
	 */
	@RequestMapping(value = "/deleteComment", method = RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> deleteComment(String id, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int num = articleService.deleteComment(id);
		if(num==1){
			map.put(Constants.REPLAY, 1);
			map.put(Constants.MESSAGE, "删除状态修改成功!");
		}else{
			map.put(Constants.REPLAY, 0);
			map.put(Constants.MESSAGE, "删除状态修改失败,请联系系统维护人员!");
		}
		return map;
	}
	
	/**
	 * 查询待审核评论数
	 */
	@RequestMapping(value = "/getWaitInspCommentNum", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getWaitInspCommentNum(HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int num = articleService.getCommentListForInspectionCount();
		if(num==0){
			map.put("num", "");
		}else{
			map.put("num", num);
		}
		map.put(Constants.REPLAY, 1);
		return map;
	}
	
	/**
	 * 查询待审核评论数
	 */
	@RequestMapping(value = "/commentIsGranted", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> commentIsGranted(String id, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int num = articleService.commentIsGranted(id);
		if(num==0){
			map.put("num", "");
		}else{
			map.put("num", num);
		}
		map.put(Constants.REPLAY, 1);
		return map;
	}
	
	/**
	 * 查询待审核评论数
	 */
	@RequestMapping(value = "/loadUnreadMsgNum", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> loadUnreadMsgNum(HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int userId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		int num = articleService.getUnreadMsgNumByUserId(userId);
		if(num==0){
			map.put("num", "");
		}else{
			map.put("num", num);
		}
		map.put(Constants.REPLAY, 1);
		return map;
	}
	
	/**
	 * 跳转到被回复消息列表
	 */
	@RequestMapping(value = "/jumpToUnreadMsgListPage", method = RequestMethod.GET)
	public ModelAndView jumpToUnreadMsgListPage(HttpSession session) {
		//点击了查看按钮后将未读的信息全部标记成已读
		int userId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		articleService.changeCommentUnreadStatus(userId);
		return new ModelAndView("article/unreadMsgList"); //TODO 这个页面可以用来显示系统提示信息... 暂时不需要做
	}
	
	/**
	 * 查询被回复的列表数据
	 */
	@RequestMapping(value = "/getSelfReplyList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getSelfReplyList(int begin, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int userId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		List<Map<String,Object>> resultList = articleService.getCommentReplyListByTargetUserId(userId,begin);
		//查询总数
		int totleNum = articleService.getCommentReplyListByTargetUserIdTotle(userId);
		map.put(Constants.REPLAY, 1);
		if(resultList.size()==0){
			map.put("list", "");
		}else{
			map.put("list", resultList);
		}
		map.put("totle", totleNum);
		return map;
	}
	
	/**
	 * 添加浏览记录信息
	 */
	@RequestMapping(value = "/addAol", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addAol(String articleId, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		int userId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		int num = articleService.addArticleOpenLog(userId,articleId);
		map.put(Constants.REPLAY, 1);
		if(num==1){
			map.put("isDone", true);
		}else{
			map.put("isDone", false);
		}
		return map;
	}
	
	/**
	 * 跳转到浏览记录列表页
	 */
	@RequestMapping(value = "/jumpToArticleOpenLogListPage", method = RequestMethod.GET)
	public ModelAndView jumpToArticleOpenLogListPageGet(HttpSession session) {
		return new ModelAndView("article/articleOpenLogList");
	}
	
	/**
	 * 浏览记录信息
	 */
	@RequestMapping(value = "/getArticleOpenLogList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getArticleOpenLogList(int page, HttpSession session) {
		int userId = Integer.parseInt(session.getAttribute(Constants.USER_ID).toString());
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String,Object>> result = new ArrayList<>();
		List<Map<String,Object>> articleOpenLogInfoList = articleService.getArticleOpenLogListByUserId(userId, page, Constants.ROW);
		SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATETIME_FORMAT);		
		for (Map<String, Object> m : articleOpenLogInfoList) {
//			articleId, title, createUser, releaseTime, openTime 
			Map<String,Object> temp = new HashMap<String,Object>();
			temp.put("articleId", m.get("articleId"));
			temp.put("title", m.get("title"));
			temp.put("createUser", m.get("createUser"));
			temp.put("releaseTime", sdf.format(m.get("releaseTime")));
			temp.put("openTime", sdf.format(m.get("openTime")));
			result.add(temp);
		}
		int totle = articleService.articleOpenLogInfoListCount(userId);
		if(result.isEmpty()){
			map.put(Constants.LIST, "");
		}else{
			map.put(Constants.LIST, result);
		}
		map.put(Constants.TOTLE, totle);
		map.put(Constants.TOTLE_PAGE, Math.ceil((double) totle / (double) Constants.ROW));
		return map;
	}
	
	
	/**
	 * 跳转文章浏览记录列表
	 */
	@RequestMapping(value = "/jumpToCheckArticleOpenLog", method = RequestMethod.GET)
	public ModelAndView jumpToCheckArticleOpenLog(HttpSession session) {
		return new ModelAndView("article/checkArticleOpenLog"); //TODO 这个页面可以用来显示系统提示信息... 暂时不需要做
	}
	
	/**
	 * 获取全部的浏览记录信息
	 */
	@RequestMapping(value = "/getAllArticleOpenLogListForOverView", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getAllArticleOpenLogListForOverView(int page, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String,Object>> result = new ArrayList<>();
		List<Map<String,Object>> articleOpenLogInfoList = articleService.getAllArticleOpenLogListForOverView(page, Constants.ROW);
		SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATETIME_FORMAT);		
		for (Map<String, Object> m : articleOpenLogInfoList) {
//			articleId, title, createUser, releaseTime, openTime 
			Map<String,Object> temp = new HashMap<String,Object>();
			temp.put("articleId", m.get("articleId"));
			temp.put("title", m.get("title"));
			temp.put("createUser", m.get("createUser"));
			temp.put("releaseTime", sdf.format(m.get("releaseTime")));
			temp.put("openTime", sdf.format(m.get("openTime")));
			temp.put("openUser", m.get("openUser"));
			result.add(temp);
		}
		int totle = articleService.getAllArticleOpenLogListForOverViewCount();
		if(result.isEmpty()){
			map.put(Constants.LIST, "");
		}else{
			map.put(Constants.LIST, result);
		}
		map.put(Constants.TOTLE, totle);
		map.put(Constants.TOTLE_PAGE, Math.ceil((double) totle / (double) Constants.ROW));
		return map;
	}
}
